package com.ruoyi.gd.detail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.detail.domain.bo.ZDetailBo;
import com.ruoyi.gd.detail.domain.vo.ZDetailVo;
import com.ruoyi.gd.detail.domain.ZDetail;
import com.ruoyi.gd.detail.mapper.ZDetailMapper;
import com.ruoyi.gd.detail.service.IZDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 发票详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-24
 */
@RequiredArgsConstructor
@Service
public class ZDetailServiceImpl implements IZDetailService {

    private final ZDetailMapper baseMapper;

    /**
     * 查询发票详情
     */
    @Override
    public ZDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询发票详情列表
     */
    @Override
    public TableDataInfo<ZDetailVo> queryPageList(ZDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZDetail> lqw = buildQueryWrapper(bo);
        Page<ZDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询发票详情列表
     */
    @Override
    public List<ZDetailVo> queryList(ZDetailBo bo) {
        LambdaQueryWrapper<ZDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZDetail> buildQueryWrapper(ZDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNumber()), ZDetail::getNumber, bo.getNumber());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZDetail::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), ZDetail::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), ZDetail::getUnit, bo.getUnit());
        lqw.eq(bo.getCount() != null, ZDetail::getCount, bo.getCount());
        lqw.eq(bo.getPrice() != null, ZDetail::getPrice, bo.getPrice());
        lqw.eq(bo.getAmount() != null, ZDetail::getAmount, bo.getAmount());
        lqw.eq(bo.getTaxRate() != null, ZDetail::getTaxRate, bo.getTaxRate());
        lqw.eq(bo.getTaxAmount() != null, ZDetail::getTaxAmount, bo.getTaxAmount());
        return lqw;
    }

    /**
     * 新增发票详情
     */
    @Override
    public Boolean insertByBo(ZDetailBo bo) {
        ZDetail add = BeanUtil.toBean(bo, ZDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发票详情
     */
    @Override
    public Boolean updateByBo(ZDetailBo bo) {
        ZDetail update = BeanUtil.toBean(bo, ZDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除发票详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
